package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.oj.ds.TreeNode
import java.util.*

/**
 * @author flutterdash@qq.com
 * @since 2021/4/7 22:20
 */
class P144 {

    private fun preorderTraversal(root: TreeNode?): List<Int> {
        if (root == null)
            return emptyList()
        val stack = LinkedList<TreeNode>();
        stack.push(root)
        val res = mutableListOf<Int>()
        while (!stack.isEmpty()) {
            val currentNode = stack.pop()
            res.add(currentNode.`val`)
            if (currentNode.right != null) stack.push(currentNode.right)
            if (currentNode.left != null) stack.push(currentNode.left)
        }
        return res
    }

    @Test
    fun test0() {
        val node1 = TreeNode(1)
        val node2 = TreeNode(2)
        val node3 = TreeNode(3)
        node1.right = node2
        node2.left = node3
        println(preorderTraversal(node1))
    }

}
